<?php 

namespace Admin\Controller;

use Think\Controller;

class IndexController extends Controller{

	public function index(){
		$this->display('index');
	}
    public function welcome(){
        $this->display('welcome');
    }

	public function actions(){
		$l = D('Login','Logic');
		$re = $l->islogin();
		if(!$re){
			redirect('/admin/login');
		}

		$status =$_GET['status'];
		$s = D('Index','Logic');
		$count = $s->countAction($status);
		$Page  = new \Think\Page($count,20);
		$show  = $Page->show();
        $show = str_replace('/index.php/','/',$show);

		$firstRow = $Page->firstRow;
		$listRows = $Page->listRows;
		$res = $s->getActions($status,$firstRow,$listRows);
		$re = $this->union($res);
		$this->assign('action',$re);
		$this->assign('page',$show);
		$this->display('data');
	}

	public function orders(){
		$l = D('Login','Logic');
		$re = $l->islogin();
		if(!$re){
			redirect('/admin/login');
		}

		$status =$_GET['status'];
		$s = D('Index','Logic');
		$count = $s->countOrder($status);
		$Page  = new \Think\Page($count,20);
		$show  = $Page->show();
        $show = str_replace('/index.php/','/',$show);
        
		$firstRow = $Page->firstRow;
		$listRows = $Page->listRows;
		$res = $s->getOrders($status,$firstRow,$listRows);
		$re = $this->union($res);
		$this->assign('count',$count);
		$this->assign('order',$re);
		$this->assign('page',$show);
		$this->display('data');
	}

	 public function search(){

	 	$s = D('Index','Logic');
	 	$l = D('Login','Logic');
		$re = $l->islogin();
		if(!$re){
			redirect('/admin/login');
		}
		$status = $_GET['status'];
        $search = $_GET['Search'];
        $store_name = trim($_GET['store']);
        $time1 = strtotime($_GET['time1']);
        $time2 = strtotime($_GET['time2'])+86399;

        if($search){
	        if(is_mobile($search)){
	            $data =array('mobile'=>$search);
	        }else{
	            $data=array('order_number'=>$search);
	        }
	    }
        if($store_name){
        	$store_na = $s->getStoreName($store_name);
            if(!$store_na){
                return;
            }
            foreach($store_na as $v){
               $_store_ids[]= $v['id']; 
            }
            $store_ids = implode(',',$_store_ids);
            $data['store_id']=array('in',$store_ids);	
        }

        if($status){
        	$statu = $status-1;
        	if($statu>=0){
                   $data['status'] = $statu; 	
        	}
        }
        if($_GET['time1'] != ''){
            $data['create_time'] = array('between',array($time1,$time2));
        }
        

        $count = $s->countSearchOrders($data);
        $Page  = new \Think\Page($count,20);
		$show  = $Page->show();
        $show = str_replace('/index.php/','/',$show);
		$firstRow = $Page->firstRow;
		$listRows = $Page->listRows;
        

        $res= $s->searchOrders($data,$firstRow,$listRows);
        $re = $this->union($res); 
        
        $this->assign('time1',$_GET['time1']);
        $this->assign('time2',$_GET['time2']);
        $this->assign('count',$count);
        $this->assign('search',$search);
        $this->assign('status',$status);
        $this->assign('store',$store_name);
        $this->assign('order',$re);
		$this->assign('page',$show);
        $this->display('data');
    }
 	
 	public function union($re){
        $s = D('Index','Logic');
        foreach($re as $k=>$v){
            $barber = $s->getBarber($v['barber_id']);
            $v['barber_id'] = $barber['name'];
            if(!empty($v['action_id'])){
            	$action = $s->getAction($v['action_id']);
            	$v['action_id'] = $action['action_name'];
            	$type = $s->getType($action['model_type']);
                $v['model_type'] = $type['model_name'];
        	}else{
                $type = $s->getType($v['model_type']);
                $v['model_type'] = $type['model_name'];
            }

            $store = $s->getStore($v['store_id']);
            $v['store_id'] = $store['name'];
            $v['store_address'] = $store['address'];
            $re[$k] = $v;
        }
        return $re;
    }

    public function prints(){
    	
    	$s = D('Index','Logic');
	 	$l = D('Login','Logic');
		$re = $l->islogin();
		if(!$re){
			redirect('/admin/login');
		}
		$status = $_POST['_status'];
        $search = $_POST['_Search'];
        $store_name = trim($_POST['_store']);
        $time1 = strtotime($_POST['time1']);
        $time2 = strtotime($_POST['time2'])+86399;

        if($time1<1 && $time1<=$time2 && $time2<1 ){
        	return false;
        }
        if($store_name){
        	$store_na = $s->getStoreName($store_name);
        	foreach($store_na as $v){
               $_store_ids[]= $v['id']; 
            }
            $store_ids = implode(',',$_store_ids);
            $data['store_id']=$store_ids;
        }

        if($status){
        	$statu = $status-1;
        	if($statu>=0){
        		$data['status'] = $statu;
        	}
        }
        $re = $s->prints($search,$data['store_id'],$data['status'],$time1,$time2);


    }


}

















	